//: strings/Groups.java
package strings;

import java.util.regex.*;

import static net.mindview.util.Print.*;

public class Groups
{
    static public final String POEM = "Twas brillig, and the slithy toves\n" + "Did gyre and gimble in the wabe.\n"
        + "All mimsy were the borogoves,\n" + "And the mome raths outgrabe.\n\n" + "Beware the Jabberwock, my son,\n"
        + "The jaws that bite, the claws that catch.\n" + "Beware the Jubjub bird, and shun\n"
        + "The frumious Bandersnatch.";
    
    public static void main(String[] args)
    {
        Matcher m = Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$").matcher(POEM);
        while (m.find())
        {
            for (int j = 0; j <= m.groupCount(); j++)
                printnb("[" + m.group(j) + "]");
            print();
        }
    }
} /*
   * Output: [the slithy toves][the][slithy toves][slithy][toves] [in the wabe.][in][the wabe.][the][wabe.] [were the
   * borogoves,][were][the borogoves,][the][borogoves,] [mome raths outgrabe.][mome][raths outgrabe.][raths][outgrabe.]
   * [Jabberwock, my son,][Jabberwock,][my son,][my][son,] [claws that catch.][claws][that catch.][that][catch.] [bird,
   * and shun][bird,][and shun][and][shun] [The frumious Bandersnatch.][The][frumious
   * Bandersnatch.][frumious][Bandersnatch.]
   */// :~
/*
 * 1.估计这个题目中的(?m)是难点。 （?modifier）是模式修饰符 比如（?i）不区分大小写匹配 模式 （?m）增强行锚点模式 。。 (?m)开启模式，直到(?-m)。如果没出现就一直匹配到最后
 * 在增强行锚点模式下$可以匹配换行符前面的位置和字符串末尾 2.\S 表示非空白字符 \s表示空白字符 +表示一个或多个 3.所以这段正则表示 从匹配文本中提取一行中的非空白字符， 而且是靠近行尾的 数据形式是： 数据 空白字符 数据
 * 空白字符 数据 结尾 追问 (?m)是不是表示按照多行模式，每次提取一行进行匹配？ 追答 恩 是多行文本模式
 */
